//
//  WBHomeViewController.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/9.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBHomeViewController.h"
#import "WBStatusListViewModel.h"
#import "WBStatusCell.h"
#import "WBRefreshControl.h"

@interface WBHomeViewController ()<UITableViewDataSource,UITableViewDelegate>
//利用这个属性去请求数据 回调的时候这个对象的数组里面已经有viewmodel了
@property (nonatomic , strong) WBStatusListViewModel *statusListViewModel;

//tableView自定义footview
@property (nonatomic , strong) UIActivityIndicatorView *activityView;

//系统的下拉刷新控件
@property (nonatomic , strong) WBRefreshControl *refreshControl;

@end

@implementation WBHomeViewController

//懒加载
- (WBStatusListViewModel *)statusListViewModel
{
    if(!_statusListViewModel)
    {
        _statusListViewModel = [[WBStatusListViewModel alloc] init];
        
    }
    
    return _statusListViewModel;
    
}

- (WBRefreshControl *)refreshControl
{
    if(!_refreshControl)
    {
        _refreshControl = [[WBRefreshControl alloc] init];
        
    }
    
    return _refreshControl;
    
}


#pragma mark 懒加载自定义footview
-(UIActivityIndicatorView *)activityView
{
    if(!_activityView)
    {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        //设置菊花的颜色
        _activityView.color = AppColor;
    }
    
    return _activityView;
    
}
#pragma mark viweDidLoad方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(!self.isLogin)
    {
        
        [self.visitorView viewWithLableText:nil andImageViewName:nil];
        
    }else{
        
        //登录了 设置tableView的相关内容
        [self setupTableViewInfo];
        
        //接收通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView) name:@"sinaSendAccess" object:nil];
        
        [self loadData];
        
    }
    
}

- (void)reloadTableView
{
    [self loadData];
    
    [self.tableView setContentOffset:CGPointMake(0, -64) animated:YES];

}

//移除通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)setupTableViewInfo
{
    //数据源代理
    self.tableView.dataSource = self;
    
    //注册cell类
    [self.tableView registerClass:[WBStatusCell class] forCellReuseIdentifier:@"cell"];
    
    // self.tableView.rowHeight = 300;
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    //设置代理
    self.tableView.delegate = self;
    
    //设置视图
    self.tableView.tableFooterView = self.activityView;
    
    //设置刷新控件的监听事件
    [self.refreshControl addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
    
    //添加控件
    [self.tableView addSubview:self.refreshControl];
    
}

#pragma mark - 刷新控件的监听事件
- (void)refreshAction
{
    NSLog(@"下拉刷新数据");
    
    [self loadData];
    
}

#pragma mark - 实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.statusListViewModel.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WBStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    //给cell赋值
    cell.statusViewModel = self.statusListViewModel.dataArray[indexPath.row];
    
    return cell;
    
}

#pragma mark - tableView的代理方法
//tableview将要显示的cell
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if((self.statusListViewModel.dataArray.count == indexPath.row + 1) && !self.activityView.isAnimating)
    {
        
        NSLog(@"最后一个cell");
        //最后一个cell 这个时候就要上拉加载更多
        NSLog(@"请求数据");
        //菊花转起来
        [self.activityView startAnimating];
        //请求数据
        [self loadData];
    }
    
}

#pragma mark - loadData请求数据
- (void)loadData
{
    [self.statusListViewModel loadHomeDataWithFinishPullUp:self.activityView.isAnimating andBlock:^(BOOL isSuccess) {
        
        [self stopAnimationAndRefreshing];
        
        
        if (!isSuccess) {
            //请求失败
            NSLog(@"请求失败 ");
            return;
        }
        
        //请求成功 刷新数据源
        [self.tableView reloadData];
        
    }];
}

- (void)stopAnimationAndRefreshing
{
    //菊花停止转动
    [self.activityView stopAnimating];
    [self.refreshControl endRefreshing];
    
}

@end
